import ast
import pandas as pd
import os
import sys

FILE_ABS_PATH = os.path.dirname(__file__)
ROOT_PATH = os.path.join(FILE_ABS_PATH, os.pardir)
sys.path.append(ROOT_PATH)
from script_convert_rating import filter_unused_data


import warnings
warnings.filterwarnings("ignore")

def convert_ratings(input_fname, output_fname):
    rating = pd.DataFrame([], columns=['userID', 'itemID', 'rating'])
    with open(input_fname, 'r') as f:
        text = f.read()
        line_l = text.split("\n")
        for i, line in enumerate(line_l, 0):
            if line == '':
                continue
            if i % 1000 == 0:
                print("i {} len {}".format(i, len(line_l)))
            jins = ast.literal_eval(line)
            if 'user_id' in jins and 'business_id' in jins and 'stars' in jins:
                rating = rating.append(
                    {'userID': jins['user_id'], 'itemID': jins['business_id'], 'rating': jins['stars']},
                    ignore_index=True)

    order_l = ['userID', 'itemID', 'rating']
    rating = rating[order_l]
    rating = filter_unused_data.convert_ratings(rating)
    filter_unused_data.stats_rating(rating)

    rating.to_csv(output_fname, index=False)
    print("complete {}".format(output_fname))


if __name__ == '__main__':
    input_fname = os.path.join('/home/bianzheng/Dataset/Recommendation/yelp/yelp_academic_dataset_review.json')
    output_fname = os.path.join('/home/bianzheng/rec2-mips/intermediate-rating-csv/yelp.csv')
    convert_ratings(input_fname, output_fname)
